package com.example.g5project.service.impl;

import com.example.g5project.entity.Supervisor;
import com.example.g5project.exception.EntityNotExistException;
import com.example.g5project.repository.repo.AdministratorRepo;
import com.example.g5project.repository.repo.SupervisorRepo;
import com.example.g5project.service.SupervisorService;
import com.example.g5project.tools.EntityUtils;
import org.assertj.core.util.Preconditions;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Optional;

/**
 * @author Hu Zirui
 * @version 1.0.0
 * @ClassName SupervisorServiceImpl.java
 * @Description TODO
 * @createTime 2022年03月09日 13:18:00
 */
@Service
@Transactional
public class SupervisorServiceImpl implements SupervisorService {
    private final SupervisorRepo supervisorRepo;
    private final AdministratorRepo administratorRepo;

    public SupervisorServiceImpl(SupervisorRepo supervisorRepo, AdministratorRepo administratorRepo) {
        this.supervisorRepo = supervisorRepo;
        this.administratorRepo = administratorRepo;
    }

    @Override
    public Supervisor getSupervisorByID(Integer id) throws EntityNotExistException {
        Preconditions.checkNotNull(id, "未输入督导ID");
        Optional<Supervisor> opt = supervisorRepo.findById(id);
        if (opt.isPresent())
            return opt.get();
        throw new EntityNotExistException("Supervisor");
    }

    @Override
    public Supervisor getSupervisorByName(String name) {
        Preconditions.checkNotNull(name, "未输入督导姓名");
        Optional<Supervisor> opt = supervisorRepo.findByName(name);
        return opt.orElse(null);
    }

    @Override
    public void addSupervisor(Supervisor Supervisor) {
        Preconditions.checkNotNull(Supervisor, "未输入督导信息");
        supervisorRepo.add(Supervisor);
    }

    @Override
    public void deleteSupervisorByID(Integer id) {
        Preconditions.checkNotNull(id, "未输入督导ID");
        administratorRepo.deleteSupervisorFromScheduleByIDAll(id);
        administratorRepo.deleteSupervisorFromBindByIDAll(id);
        supervisorRepo.deleteById(id);
    }

    @Override
    public void updateSupervisorByID(Integer id, Supervisor Supervisor) throws EntityNotExistException {
        Preconditions.checkNotNull(Supervisor, "未输入督导信息");
        Preconditions.checkNotNull(id, "未输入督导ID");
        supervisorRepo.updateById(id, Supervisor);
    }

    @Override
    public ArrayList<Supervisor> getAllSupervisor() {
        return supervisorRepo.findAll();
    }

    @Override
    public Integer getTodayConsultNumber(Integer id) {
        Preconditions.checkNotNull(id, "未输入督导ID");
        return supervisorRepo.getTodayConsultNumber(id);
    }

    @Override
    public String getTodayConsultTime(Integer id) {
        Preconditions.checkNotNull(id, "未输入督导ID");
        return supervisorRepo.getTodayConsultTime(id);
    }

    @Override
    public Integer getAllConsultSessionNumber() {
        return supervisorRepo.getAllConsultSessionNumber();
    }

    @Override
    public void addAliveSessionNumber(Integer id) {
        Preconditions.checkNotNull(id, "未输入督导ID");
        supervisorRepo.addAliveSessionNumber(id);
    }

    @Override
    public void addTodaySessionNumber(Integer id) {
        Preconditions.checkNotNull(id, "未输入督导ID");
        supervisorRepo.addTodaySessionNumber(id);
    }

    @Override
    public ArrayList<String> getScheduleByMonth(String name, String month) {
        return supervisorRepo.getScheduleByMonth(name, month);
    }

    @Override
    public void increAllConsultNumber(String name) {
        supervisorRepo.increAllConsultNumber(name);
    }

    @Override
    public void incretodayConsultNumber(String name) {
        supervisorRepo.incretodayConsultNumber(name);
    }

    @Override
    public void increTodayConsultTime(String name, String time) {
        Optional<Supervisor> supervisor = supervisorRepo.findByName(name);
        if (supervisor.isPresent()) {
            System.out.println(supervisor.get().getName());
            String hold_time = supervisor.get().getToday_consult_time();
            System.out.println(hold_time);
            if (hold_time == null || hold_time.equals("null"))
                hold_time = "00:00:00";
            EntityUtils entityUtils = new EntityUtils();
            String new_hold_time = entityUtils.transformDate(hold_time, time);
            System.out.println(new_hold_time);
            supervisorRepo.updatetodayHoldTimeByName(name, new_hold_time);
        }
    }

    @Override
    public void increAllConsultTime(String name, String time) {
        Optional<Supervisor> supervisor = supervisorRepo.findByName(name);
        if (supervisor.isPresent()) {
            String hold_time = supervisor.get().getToday_consult_time();
            if (hold_time == null || hold_time.equals("null"))
                hold_time = "00:00:00";
            EntityUtils entityUtils = new EntityUtils();
            String new_hold_time = entityUtils.transformDate(hold_time, time);
            supervisorRepo.updateallHoldTimeByName(name, new_hold_time);
        }
    }

    @Override
    public void setStatus(String name) {
        supervisorRepo.setStatus(name);
    }
}